<?php
class ModelLocalisationLanguage extends Model {
	public function addLanguage($data) {
		$this->db->query("INSERT INTO " . DB_PREFIX . "language SET name = '" . $this->db->escape($data['name']) . 
			"', code = '" . $this->db->escape($data['code']) . 
			"', locale = '" . $this->db->escape($data['locale']) . 
			"', directory = '" . $this->db->escape($data['directory']) . 
			"', filename = '" . $this->db->escape($data['filename']) . 
			"', image = '" . $this->db->escape($data['image']) . 
			"', sort_order = '" . $this->db->escape($data['sort_order']) . 
			"', status = '" . (int)$data['status'] . "'");
	}
	
	public function editLanguage($language_id, $data) {
		$this->db->query("UPDATE " . DB_PREFIX . "language SET name = '" . $this->db->escape($data['name']) . 
			"', code = '" . $this->db->escape($data['code']) . 
			"', locale = '" . $this->db->escape($data['locale']) . 
			"', directory = '" . $this->db->escape($data['directory']) . 
			"', filename = '" . $this->db->escape($data['filename']) . 
			"', image = '" . $this->db->escape($data['image']) . 
			"', sort_order = '" . $this->db->escape($data['sort_order']) . 
			"', status = '" . (int)$data['status'] . 
			"' WHERE language_id = '" . (int)$language_id . "'");
	}
	
	public function getLanguage($language_id) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "language WHERE language_id = '" . (int)$language_id . "'");
		return $query->row;
	}
	
	public function getLangIdAndName() {
		$query = $this->db->query("SELECT language_id, name FROM " . DB_PREFIX . "language");
		return $query->rows;
	}

	public function getLanguages($data = array()) {
		if ($data) {
			$sql = "SELECT * FROM " . DB_PREFIX . "language ORDER BY sort_order, name";
			if (isset($data['start']) || isset($data['limit'])) {
				if ($data['start'] < 0) {
					$data['start'] = 0;
				}
				if ($data['limit'] < 1) {
					$data['limit'] = 20;
				}
				$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
			}
			$query = $this->db->query($sql);
			return $query->rows;
		} else {
			$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "language ORDER BY sort_order, name");
			$language_data = array();
    		foreach ($query->rows as $result) {
      			$language_data[$result['code']] = array(
        			'language_id' => $result['language_id'],
        			'name'        => $result['name'],
        			'code'        => $result['code'],
					'locale'      => $result['locale'],
					'image'       => $result['image'],
					'directory'   => $result['directory'],
					'filename'    => $result['filename'],
					'sort_order'  => $result['sort_order'],
					'status'      => $result['status']
      			);
    		}
			return $language_data;			
		}
	}

	public function getTotalLanguages() {
      	$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "language");
		return $query->row['total'];
	}
	
	public function allLanguage() {
		$query = $this->db->query("SELECT language_id, `name`, `code`, image FROM " . DB_PREFIX . "language ORDER BY sort_order, name");
		return $query->rows;
	}
	
	/**
	 *key: id, code 
	 */
	public function allLanguageHasKey($key = 'id') {
		$query = $this->db->query("SELECT language_id, `name`, `code`, image FROM " . DB_PREFIX . "language ORDER BY sort_order, name");
		$language_data = array();
		if($key == 'code'){
	    	foreach ($query->rows as $result) {
	      		$language_data[$result['code']] = array(
	        		'language_id' => $result['language_id'],
	        		'name'        => $result['name'],
	        		'code'        => $result['code'],
					'image'       => $result['image']
	      		);
	    	}
		}else{
			foreach ($query->rows as $result) {
	      		$language_data[$result['language_id']] = array(
	        		'language_id' => $result['language_id'],
	        		'name'        => $result['name'],
	        		'code'        => $result['code'],
					'image'       => $result['image']
	      		);
	    	}
    	}
		return $language_data;
	}
	
}
?>